
#event study functions----------------------------------------------------------------------
make_twfe_event_study <- function(df, govt_prop, pop_prop, occ_filter, lab1, lab2){
  var_govt_prop = enquo(govt_prop)
  var_pop_prop = enquo(pop_prop)
  
  df <-
    df %>%
    filter_(occ_filter) %>%
    mutate(
      #outcome variables
      govt_prop = !!var_govt_prop,
      pop_prop = !!var_pop_prop,
      over_prop = govt_prop - pop_prop,
      
      #independent variables    
      ever_treat = ((!is.na(treat))*1),
      distance = case_when(ever_treat == 0 ~ 0,
                           TRUE ~ as.numeric(YEAR) - as.numeric(all)),
      grouped_distance = floor(distance/10))
  
  feols(govt_prop ~ i(grouped_distance, ever_treat, ref = -1) + pop_prop | city + YEAR, cluster = ~city, data = df) %>%
    tidy() %>%
    select(term, estimate, std.error) %>%
    add_row(term = "-1",  estimate = 0, std.error = NA_real_) %>%
    mutate(val = str_remove_all(term, "[:alpha:]|\\_|\\:"),
           lab1 = lab1,
           lab2 = lab2)
}

summary(factor(analysis_df_3$occ))

#blue collar plots----------------------------------------------------------------------
po_out1 <- make_twfe_event_study(analysis_df_3, govt_white_x_foreign_born, white_x_foreign_born, 'occ == "police"', lab1 = "White Foreign Born", lab2 = "Police")
po_out2 <- make_twfe_event_study(analysis_df_3, govt_white_x_native_born, white_x_native_born, 'occ == "police"', lab1 = "White Native Born", lab2 = "Police")
po_out3 <- make_twfe_event_study(analysis_df_3, govt_black_x_native_born, black_x_native_born, 'occ == "police"', lab1 = "Black Native Born", lab2 = "Police")

po_out4 <- make_twfe_event_study(analysis_df_4, govt_irish, irish, 'occ == "police"', lab1 = "Irish", lab2 = "Police")
po_out5 <- make_twfe_event_study(analysis_df_4, govt_german, german, 'occ == "police"', lab1 = "German", lab2 = "Police")
po_out6 <- make_twfe_event_study(analysis_df_4, govt_italian, italian, 'occ == "police"', lab1 = "Italian", lab2 = "Police")
po_out7 <- make_twfe_event_study(analysis_df_4, govt_polish, polish, 'occ == "police"', lab1 = "Polish", lab2 = "Police")
po_out8 <- make_twfe_event_study(analysis_df_4, govt_russian, russian, 'occ == "police"', lab1 = "Russian", lab2 = "Police")

#white collar plots----------------------------------------------------------------------
fi_out1 <- make_twfe_event_study(analysis_df_3, govt_white_x_foreign_born, white_x_foreign_born, 'occ == "fireman"', lab1 = "White Foreign Born", lab2 = "Fire Dept")
fi_out2 <- make_twfe_event_study(analysis_df_3, govt_white_x_native_born, white_x_native_born, 'occ == "fireman"', lab1 = "White Native Born", lab2 = "Fire Dept")
fi_out3 <- make_twfe_event_study(analysis_df_3, govt_black_x_native_born, black_x_native_born, 'occ == "fireman"', lab1 = "Black Native Born", lab2 = "Fire Dept")

fi_out4 <- make_twfe_event_study(analysis_df_4, govt_irish, irish, 'occ == "fireman"', lab1 = "Irish", lab2 = "Fire Dept")
fi_out5 <- make_twfe_event_study(analysis_df_4, govt_german, german, 'occ == "fireman"', lab1 = "German", lab2 = "Fire Dept")
fi_out6 <- make_twfe_event_study(analysis_df_4, govt_italian, italian, 'occ == "fireman"', lab1 = "Italian", lab2 = "Fire Dept")
fi_out7 <- make_twfe_event_study(analysis_df_4, govt_polish, polish, 'occ == "fireman"', lab1 = "Polish", lab2 = "Fire Dept")
fi_out8 <- make_twfe_event_study(analysis_df_4, govt_russian, russian, 'occ == "fireman"', lab1 = "Russian", lab2 = "Fire Dept")


main_plot <-
  bind_rows(po_out1, po_out2, po_out3,
            fi_out1, fi_out2, fi_out3) %>%
  mutate(val = as.numeric(as.character(val))) %>%
  mutate(pre_post = (val > -1)*1) %>%
  mutate(lab1 = factor(lab1, levels = c("White Foreign Born", "Black Native Born", "White Native Born"))) %>%
  ggplot(aes(x=val, y = estimate, shape = lab1, color = lab1, group = lab1, alpha = factor(pre_post))) +
  geom_point(position = position_dodge(width = 0.5)) +
  geom_errorbar(aes(ymin = estimate - 1.96*std.error, ymax = estimate + 1.96*std.error), 
                position = position_dodge(width = 0.5),
                width = 0) +
  geom_hline(yintercept = 0, linetype = "dotted") +
  facet_wrap(lab2~.) +
  theme_bw() +
  scale_colour_grey() +
  xlab("Decades Before/After Civil Service Reform") +
  ylab("Effect of Civil Service Reform on Representation") +
  theme(panel.grid.minor = element_blank(), 
        panel.grid.major.x = element_blank(),
        axis.line.y.left = element_blank(),
        legend.position = "bottom",
        strip.background = element_blank(),
        legend.title = element_blank(),
        axis.line = element_line(colour = "black"),
        panel.border = element_blank()) +
  scale_x_continuous(breaks = seq(-4, 4, by = 1), 
                     limits = c(-4.5, 4.5)) +
  scale_alpha_manual(values = c(0.4, 1)) +
  guides(alpha = FALSE) +
  ylim(-0.15, 0.15) +
theme(text=element_text(size=9, family="lmroman"))



whites_plot <-
  bind_rows(po_out4, po_out5, po_out6, po_out7, po_out8,
            fi_out4, fi_out5, fi_out6, fi_out7, fi_out8) %>%
  mutate(val = as.numeric(as.character(val))) %>%
  mutate(lab1 = factor(lab1, levels = c("Irish", "German", "Italian", "Polish", "Russian"))) %>%
  mutate(pre_post = (val > -1)*1) %>%
  ggplot(aes(x=val, y = estimate, shape = lab1, color = lab1, group = lab1, alpha = factor(pre_post))) +
  geom_point(position = position_dodge(width = 0.5)) +
  geom_errorbar(aes(ymin = estimate - 1.96*std.error, ymax = estimate + 1.96*std.error), 
                position = position_dodge(width = 0.5),
                width = 0) +
  geom_hline(yintercept = 0, linetype = "dotted") +
  facet_wrap(lab2~.) +
  theme_bw() +
  scale_colour_grey() +
  xlab("Decades Before/After Civil Service Reform") +
  ylab("Effect of Civil Service Reform on Representation") +
  theme(panel.grid.minor = element_blank(), 
        panel.grid.major.x = element_blank(),
        axis.line.y.left = element_blank(),
        legend.position = "bottom",
        strip.background = element_blank(),
        legend.title = element_blank(),
        axis.line = element_line(colour = "black"),
        panel.border = element_blank()) +
  scale_x_continuous(breaks = seq(-4, 4, by = 1), 
                     limits = c(-4.5, 4.5)) +
  scale_alpha_manual(values = c(0.4, 1)) +
  guides(alpha = FALSE) +
  ylim(-0.1, 0.1) +
theme(text=element_text(size=9, family="lmroman"))


#saving plots-------------------------------------------------------------------------

ggsave('./replication_file/_5_outputs/figures/figure_a4.png', plot = main_plot, width=6, height=4)
ggsave('./replication_file/_5_outputs/figures/figure_a5.png', plot = whites_plot, width=6, height=4)

#ggsave('../Apps/Overleaf/merit paper/outputs/po_fi_main_plot.png', plot = main_plot, width=6, height=4)
#ggsave('../Apps/Overleaf/merit paper/outputs/po_fi_whites_plot.png', plot = whites_plot, width=6, height=4)






